package myapps.ttclient.activity.ppt;

import java.text.DecimalFormat;

import myapps.ttclient.GlobalVariables;
import myapps.ttclient.Helper;
import myapps.ttclient.R;
import myapps.ttclient.app.TTClientApplication;
import myapps.ttclient.baseclass_and_interface.TTClientMessage;
import myapps.ttclient.baseclass_and_interface.TTClientMessageReceiver;
import myapps.ttclient.protocol.message.FileDetailsRequestMessage;
import myapps.ttclient.protocol.message.FileDetailsResponseMessage;
import myapps.ttclient.protocol.message.powerpoint.PPTOpenFileRequestMessage;
import myapps.ttclient.protocol.message.powerpoint.PPTTCPSlideShowScreenCaptureMessage;
import myapps.ttclient.struct.FileDetailsStruct;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class PPTFileDetailsActivity extends Activity implements TTClientMessageReceiver {

	private TTClientApplication application;
	
	private ImageView ivIcon;
	
	private TextView tvName;
	private TextView tvLenght;
	private TextView tvDirectoryFullPath;
	private TextView tvCreationTime;
	private TextView tvLastAccessTime;
	private TextView tvLastWriteTime;
	
	private Button btnOpen;
	private Button btnClose;
	
	private String pptFilePath = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/////////////////////////////////////////////////////////
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		Helper.updateFullscreenStatus(this, true);
		Helper.keepScreenOn(this);

		this.application = (TTClientApplication) this.getApplication();

		setContentView(R.layout.file_details);
		
		ivIcon = (ImageView) this.findViewById(R.id.icon);
		
		tvName = (TextView) this.findViewById(R.id.fdName);
		tvLenght = (TextView) this.findViewById(R.id.fdLength);
		tvDirectoryFullPath = (TextView) this.findViewById(R.id.fdDirectoryFullPath);
		tvCreationTime = (TextView) this.findViewById(R.id.fdCreationTime);
		tvLastAccessTime = (TextView) this.findViewById(R.id.fdLastAccessTime);
		tvLastWriteTime = (TextView) this.findViewById(R.id.fdLastWriteTime);
		
		btnOpen = (Button) this.findViewById(R.id.btnOpen);
		btnClose = (Button) this.findViewById(R.id.btnClose);
		
		btnOpen.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				PPTFileDetailsActivity.this.application.sendMessage(
						new PPTOpenFileRequestMessage(GlobalVariables.getTemporaryKey(), 
								PPTOpenFileActivity.FILE_EXPLORER_FILE	, pptFilePath));
				
			}
		});
		
		btnClose.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				PPTFileDetailsActivity.this.finish();
				
			}
		});

		try
		{
			Intent indent = this.getIntent();
			String filePath = indent.getStringExtra("FILE_PATH");
			pptFilePath = filePath;
			this.application.sendMessage(new FileDetailsRequestMessage(filePath));
		}
		catch (Exception ex) {

		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.application.registerMessageReceiver(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		this.application.unregisterMessageReceiver(this);
	}

	@Override
	public void receiveMessage(TTClientMessage message) {
		if (message instanceof FileDetailsResponseMessage) {
			FileDetailsResponseMessage msg = (FileDetailsResponseMessage) message;
			
			final FileDetailsStruct fd = msg.getFileDetails();
			
			this.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					
					if(fd.name.endsWith(".pptx")) {
						ivIcon.setImageResource(R.drawable.ic_file_explorer_ppt);
						
					} else if(fd.name.endsWith(".ppt")) {
						ivIcon.setImageResource(R.drawable.ic_file_explorer_ppt);
						
					}  else {
						ivIcon.setImageResource(R.drawable.ic_file_explorer_default_file);
					}
					
					tvName.setText(fd.name);
					tvLenght.setText(convertToProperUnit(fd.length));
					tvDirectoryFullPath.setText(fd.directoryFullPath);
					tvCreationTime.setText(fd.creationTime);
					tvLastAccessTime.setText(fd.lastAccessTime);
					tvLastWriteTime.setText(fd.lastWriteTime);
					
				}
			});
			
		}
	}
	
	public String convertToProperUnit(long size) {
		String dv = "B";
		float s = (float) size;
		
		if(size > 1024) {
			s /= 1024;
			dv = "KB";
			
			if(s > 1024) {
				s /= 1024;
				dv = "MB";
			}
		}
		
		DecimalFormat dec = new DecimalFormat("###.##");
		
		return dec.format(s) + " " + dv ;
	}

}
